clear all

cd "$US_Ineq_Repl"

local Data "$US_Ineq_Repl/Processed/CEPR"
local Tables "$US_Ineq_Repl/Results/Tables"
cd `Data'

local year_b=2020

foreach year in 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 /*
			*/	1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 /*
			*/  1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 /*
			*/  2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 /*
			*/  2019 2020 {
	*minimum wage and CPI
	
	use "$US_Ineq_Repl/Processed/CPI_mwage.dta", replace

	local row=`year'-1977
	local cpi=cpi in `row'
	local mwage=minwage in `row'

	local row=`year_b'-1977
	local cpi_b=cpi in `row'
	local row=1979-1977
	local cpi79=cpi in `row'
	
	local lower=1*(`cpi'/`cpi79')
	local upper=100*(`cpi'/`cpi79')
	di `lower'
	di `upper'
	
	use "cepr_org_`year'.dta", replace
	*transform to `year_b' usd!
	gen rwage3=wage3*(`cpi_b'/`cpi')
	gen rmwage = `mwage'*(`cpi_b'/`cpi')
	keep if 16<=age & age<=65
	keep if `lower'<=rwage3 & rwage3 <=`upper'
	keep if rwage3!=.

	*experience is a transformation of age and years of education
	gen exper=age-years_educ-5
	keep if exper>=0
	lab var exper "Potential experience"
	notes exper: Potencial experience is a transformation of age and years of education (age - years_educ-5)
	
	gen nonwhite=0
	replace nonwhite=1 if wbho!=1
	lab var nonwhite "Race different than white"
	#delimit ;
	lab define nonwhite
	0 "white"
	1 "nonwhite"
	, add
	;
	#delimit cr
	lab val nonwhite nonwhite
	tab nonwhite
	

	gen educ_cl=1 if 0<=years_educ & years_educ<=11
	replace educ_cl=2 if years_educ==12
	replace educ_cl=3 if years_educ==13 | years_educ==15
	replace educ_cl=4 if years_educ==14
	replace educ_cl=5 if 16<=years_educ
	
	lab var educ_cl "Levels of Education"
	#delimit ;
	lab define educ_cl
	1 "Less Than High School"
	2 "High School"
	3 "Some College"
	4 "Associate Degree"
	5 "College or more"
	, add
	;
	#delimit cr
	lab val educ_cl educ_cl
	tab educ_cl
	

	gen exp_cl=1 if 0<=exper & exper< 10
	replace exp_cl=2 if 10<=exper & exper< 20
	replace exp_cl=3 if 20<=exper & exper< 30
	replace exp_cl=4 if 30<=exper 
	
	lab var exp_cl "Levels of Experience"
	#delimit ;
	lab define exp_cl
	1 "Less than 10 years"
	2 "Between 10 and 20 years"
	3 "Between 20 and 30 years"
	4 "30 or more years"
	, add
	;
	#delimit cr
	lab val exp_cl exp_cl
	tab exp_cl
	

	local k=0
	gen ee_cl=0
	forvalues i = 1(1)5{
		forvalues j = 1(1)4{
			local k=`k'+1
			count if educ_cl==`i' & exp_cl==`j'
			replace ee_cl=`k' if educ_cl==`i' & exp_cl==`j'
		}
	}
	tab ee_cl

	rename metro smsa
	rename ptecon partte

	gen reg1=0
	replace reg1=1 if state==11 | state==12 | state==13 |  state==14 |  state==15 | /*
			   */state==16 | state==21 | state==22 | state==23
	lab var reg1 "Northeast"

	gen reg2=0
	replace reg2=1 if state==31 | state==32 | state==33 | state==34 | state==35 | /*
			   */state==41 | state==42 | state==43 | state==44 | state==45 | state==46 | state==47
	lab var reg2 "Midwest"

	gen reg3=0
	replace reg3=1 if state==51 | state==52 | state==53 | state==54 | state==55 | /*
			   */state==56 | state==57 | state==58 | state==59 | state==61 | state==62 |/*
			   */state==63 | state==64 | state==71 | state==72 | state==73 | state==74 
	lab var reg3 "South"

	gen reg4=0
	replace reg4=1 if state==81 | state==82 | state==83 | state==84 | state==85 | /*
			   */state==86 | state==87 | state==88 | state==91 | state==92 | state==93|/*
			   */state==94 | state==95
	lab var reg4 "West"
	
	gen region=.
	replace region=1 if reg1==1
	replace region=2 if reg2==1
	replace region=3 if reg3==1
	replace region=4 if reg4==1
	
	lab var region "Regions"
	#delimit ;
	lab define region
	1 "Northeast"
	2 "Midwest"
	3 "South"
	4 "West"
	, add
	;
	#delimit cr
	lab val region region
	tab region
	
	gen edex=years_educ*exper

	count if ind_nber!=.
	if `r(N)'>0{
		gen ind21=1 if ind_nber==1
		replace ind21=1 if ind_nber==2
		replace ind21=2 if ind_nber==3
		replace ind21=3 if ind_nber==4
		replace ind21=6 if ind_nber==5
		replace ind21=4 if ind_nber==6
		replace ind21=4 if ind_nber==7
		replace ind21=4 if ind_nber==8
		replace ind21=4 if ind_nber==9
		replace ind21=4 if ind_nber==10
		replace ind21=4 if ind_nber==11
		replace ind21=4 if ind_nber==12
		replace ind21=4 if ind_nber==13
		replace ind21=4 if ind_nber==14
		replace ind21=4 if ind_nber==15
		replace ind21=6 if ind_nber==16
		replace ind21=4 if ind_nber==17
		replace ind21=4 if ind_nber==18
		replace ind21=18 if ind_nber==19
		replace ind21=5 if ind_nber==20
		replace ind21=5 if ind_nber==21
		replace ind21=5 if ind_nber==22
		replace ind21=5 if ind_nber==23
		replace ind21=10 if ind_nber==24
		replace ind21=5 if ind_nber==25
		replace ind21=5 if ind_nber==26
		replace ind21=5 if ind_nber==27
		replace ind21=5 if ind_nber==28
		replace ind21=8 if ind_nber==29
		replace ind21=10 if ind_nber==30
		replace ind21=9 if ind_nber==31
		replace ind21=6 if ind_nber==32
		replace ind21=7 if ind_nber==33
		replace ind21=11 if ind_nber==34
		replace ind21=12 if ind_nber==35
		replace ind21=19 if ind_nber==36
		replace ind21=14 if ind_nber==37
		replace ind21=19 if ind_nber==38
		replace ind21=19 if ind_nber==39
		replace ind21=17 if ind_nber==40
		replace ind21=16 if ind_nber==41
		replace ind21=16 if ind_nber==42
		replace ind21=15 if ind_nber==43
		replace ind21=16 if ind_nber==44
		replace ind21=13 if ind_nber==45
		replace ind21=1 if ind_nber==46
		replace ind21=21 if ind_nber==51
		replace ind21=20 if ind_nber==52

	}
	else{
		gen ind21=1 if ind_2d==1
		replace ind21=1 if ind_2d==2
		replace ind21=2 if ind_2d==3
		replace ind21=3 if ind_2d==4
		replace ind21=4 if ind_2d==5
		replace ind21=4 if ind_2d==6
		replace ind21=4 if ind_2d==7
		replace ind21=4 if ind_2d==8
		replace ind21=4 if ind_2d==9
		replace ind21=4 if ind_2d==10
		replace ind21=4 if ind_2d==11
		replace ind21=4 if ind_2d==12
		replace ind21=4 if ind_2d==13
		replace ind21=5 if ind_2d==14
		replace ind21=5 if ind_2d==15
		replace ind21=5 if ind_2d==16
		replace ind21=5 if ind_2d==17
		replace ind21=5 if ind_2d==18
		replace ind21=5 if ind_2d==19
		replace ind21=5 if ind_2d==20
		replace ind21=6 if ind_2d==21
		replace ind21=7 if ind_2d==22
		replace ind21=8 if ind_2d==23
		replace ind21=9 if ind_2d==24
		replace ind21=10 if ind_2d==25
		replace ind21=10 if ind_2d==26
		replace ind21=10 if ind_2d==27
		replace ind21=10 if ind_2d==28
		replace ind21=10 if ind_2d==29
		replace ind21=10 if ind_2d==30
		replace ind21=10 if ind_2d==31
		replace ind21=11 if ind_2d==32
		replace ind21=11 if ind_2d==33
		replace ind21=12 if ind_2d==34
		replace ind21=12 if ind_2d==35
		replace ind21=13 if ind_2d==36
		replace ind21=14 if ind_2d==37
		replace ind21=14 if ind_2d==38
		replace ind21=14 if ind_2d==39
		replace ind21=15 if ind_2d==40
		replace ind21=16 if ind_2d==41
		replace ind21=16 if ind_2d==42
		replace ind21=16 if ind_2d==43
		replace ind21=17 if ind_2d==44
		replace ind21=18 if ind_2d==45
		replace ind21=18 if ind_2d==46
		replace ind21=19 if ind_2d==47
		replace ind21=19 if ind_2d==48
		replace ind21=19 if ind_2d==49
		replace ind21=19 if ind_2d==50
		replace ind21=20 if ind_2d==51
		replace ind21=21 if ind_2d==52
	} 
	lab var ind21 "21 Industries Clasification"
	#delimit ;
	lab define ind21
	1 "Agriculture, Forestry, Fishing, and Hunting"
	2 "Mining"
	3 "Construction"
	4 "Durable Goods Manufacturing"
	5 "Nondurable Goods manufacturing"
	6 "Durable Goods Wholesale"
	7 "Retail Trade"
	8 "Transportation and Warehousing"
	9 "Utilities"
	10 "Information"
	11 "Finance and Insurance"
	12 "Real Estate and Rental and Leasing"
	13 "Professional, Scientific, and Technical Services"
	14 "Management, Administrative and Support, and Waste Management Services"
	15 "Educational Services"
	16 "Health Care and Social Assistance"
	17 "Arts, Entertainment, and Recreation"
	18 "Accommodation and Food Service"
	19 "Other Services (Except Public Administration)"
	20 "Public Administration"
	21 "Armed Forces"
	, add
	;
	#delimit cr
	lab val ind21 ind21
	tab ind21

	keep  year month minsamp hhid hrlonglk lineno /*
		*/fnlwgt orgwgt lonwgt famwgt /*
		*/age female wbho married lfstat /*
		*/empl nilf selfemp selfinc /*
		*/pubsect pubfed pubst publoc /*
		*/union state smsa educ years_educ /*
		*/ind_nber ind21 /*
		*/agric manuf servs partte /*
		*/wage1 wage2 wage3 wage4 rwage3 tc /*
		*/exper nonwhite educ_cl exp_cl ee_cl reg1 reg2 reg3 reg4 region edex rmwage
	
	save "`year'temp.dta", replace
}
use "1979temp.dta", replace
erase "1979temp.dta"
foreach year in 1980 1981 1982 1983 1984 1985 1986 1987 1988 /*
			*/	1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 /*
			*/  1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 /*
			*/  2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 /*
			*/  2019 2020 {
	append using "`year'temp.dta"
	erase "`year'temp.dta"
}
save "$US_Ineq_Repl/Processed/DATA.dta", replace

***
*** Summary Statistics
***

use "$US_Ineq_Repl/Processed/DATA.dta", replace
*log of real wage
gen lrwage3=ln(rwage3) 

/// Excel Summary Stats full sample
{
	preserve
	collapse  (mean) Real_Min_Wage=rmwage (mean) Real_Wage=rwage3 /*
			*/(mean) Manufacturing=manuf /*
			*/(mean) Union=union (mean) Nonwhite=nonwhite /*
			*/(mean) Education=years_educ (mean) Experience=exper (count) No_Obs=lrwage3 /*
			*/(p1) p1=lrwage3 (p10) p10=lrwage3 (p25) p25=lrwage3 (p50) p50=lrwage3 /*
			*/(p75) p75=lrwage3 (p90) p90=lrwage3 (p99) p99=lrwage3 [w=orgwgt], by(year)

	gen Log_Real_Wage=ln(Real_Wage)
	gen Log_Real_Min_Wage=ln(Real_Min_Wage)

	keep  Log_Real_Min_Wage Log_Real_Wage Manufacturing /*
		*/Union Nonwhite Education Experience No_Obs p1 p10 p25 p50 p75 p90 p99
	order Log_Real_Min_Wage Log_Real_Wage Manufacturing /*
		*/Union Nonwhite Education Experience No_Obs p1 p10 p25 p50 p75 p90 p99
	ds
	mkmat `r(varlist)' , mat(S)
	cd `Tables'
	xml_tab S, save("Summary_Stat.xls") sheet(All) replace
	restore
}

/// Table Summary Stats full sample
{
	clear matrix
	*ssc install gtools, replace
	estpost gtabstat rmwage [w=orgwgt], statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'

	mata: st_matrix("ltemp1", log(st_matrix("temp1")))
	mat temp1 = temp1,ltemp1
	mat temp1 = temp1[1 .. rowsof(temp1), 2]
	matrix colnames temp1 = Log_Real_Min_Wage
	matrix output = temp1

	mat temp1 = temp1'
	estadd matrix LogRmw temp1
	eststo LogRmw 

	estpost gtabstat rwage3 [w=orgwgt], statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'

	mata: st_matrix("ltemp1", log(st_matrix("temp1")))
	mat temp1 = temp1,ltemp1
	mat temp1 = temp1[1 .. rowsof(temp1), 2]
	matrix colnames temp1 = Log_Real_Wage
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix LogRw temp1
	eststo LogRw 
	
	estpost gtabstat manuf [w=orgwgt], statistics(mean) by(year)
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Manufacturing
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix Manufacturing temp1
	eststo Manufacturing
	
	estpost gtabstat union [w=orgwgt], statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Union
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix Union temp1
	eststo Union

	estpost gtabstat nonwhite [w=orgwgt], statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Nonwhite
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix Nonwhite temp1
	eststo Nonwhite

	estpost gtabstat years_educ [w=orgwgt], statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Education
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix Education temp1
	eststo Education

	estpost gtabstat exper [w=orgwgt], statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Experience
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix Experience temp1
	eststo Experience

	estpost gtabstat lrwage3, statistics(count) by(year) 
	mat temp1 = e(n)
	mat temp1 = temp1'
	matrix colnames temp1 = No_Obs
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix Obs temp1
	eststo Obs

	#delimit ;
	esttab LogRmw LogRw Manufacturing Union Nonwhite Education Experience Obs,
	cells("LogRmw(pattern(1 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Real Min. Wage)) 
			LogRw(pattern(0 1 0 0 0 0 0 0) fmt(%20.2fc) lab(Real Wage))
	Manufacturing(pattern(0 0 1 0 0 0 0 0) fmt(%20.2fc) lab(Manufacturing.))
	        Union(pattern(0 0 0 1 0 0 0 0) fmt(%20.2fc) lab(Union))
		 Nonwhite(pattern(0 0 0 0 1 0 0 0) fmt(%20.2fc) lab(Nonwhite))
		Education(pattern(0 0 0 0 0 1 0 0) fmt(%20.2fc) lab(Education))
	   Experience(pattern(0 0 0 0 0 0 1 0) fmt(%20.2fc) lab(Experience))
			  Obs(pattern(0 0 0 0 0 0 0 1) fmt(%20.0fc) lab(No. Obs.))")
			  noobs mlabels(,none)
			  nostar nonum nogaps nodepvars compress ;
	#delimit cr

}

/// Excel Summary Stats males
{
	preserve
	collapse  (mean) Real_Min_Wage=rmwage (mean) Real_Wage=rwage3 /*
			*/(mean) Manufacturing=manuf /*
			*/(mean) Union=union (mean) Nonwhite=nonwhite /*
			*/(mean) Education=years_educ (mean) Experience=exper (count) No_Obs=lrwage3 /*
			*/(p1) p1=lrwage3 (p10) p10=lrwage3 (p25) p25=lrwage3 (p50) p50=lrwage3 /*
			*/(p75) p75=lrwage3 (p90) p90=lrwage3 (p99) p99=lrwage3 [w=orgwgt] if female==0, by(year)

	gen Log_Real_Wage=ln(Real_Wage)
	gen Log_Real_Min_Wage=ln(Real_Min_Wage)

	keep Log_Real_Min_Wage Log_Real_Wage Manufacturing Union Nonwhite Education Experience No_Obs p1 p10 p25 p50 p75 p90 p99
	order Log_Real_Min_Wage Log_Real_Wage Manufacturing Union Nonwhite Education Experience No_Obs p1 p10 p25 p50 p75 p90 p99
	ds
	mkmat `r(varlist)' , mat(S)
	cd `Tables'
	xml_tab S, save("Summary_Stat.xls") sheet(Males) append
	restore
}

/// Table Summary Stats males
{
	estpost gtabstat rmwage [w=orgwgt] if female==0, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'

	mata: st_matrix("ltemp1", log(st_matrix("temp1")))
	mat temp1 = temp1,ltemp1
	mat temp1 = temp1[1 .. rowsof(temp1), 2]
	matrix colnames temp1 = Log_Real_Min_Wage
	matrix output = temp1

	mat temp1 = temp1'
	estadd matrix LogRmwM temp1
	eststo LogRmwM 

	estpost gtabstat rwage3 [w=orgwgt] if female==0, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'

	mata: st_matrix("ltemp1", log(st_matrix("temp1")))
	mat temp1 = temp1,ltemp1
	mat temp1 = temp1[1 .. rowsof(temp1), 2]
	matrix colnames temp1 = Log_Real_Wage
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix LogRwM temp1
	eststo LogRwM 

	estpost gtabstat union [w=orgwgt] if female==0, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Union
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix UnionM temp1
	eststo UnionM

	estpost gtabstat nonwhite [w=orgwgt] if female==0, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Nonwhite
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix NonwhiteM temp1
	eststo NonwhiteM

	estpost gtabstat years_educ [w=orgwgt] if female==0, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Education
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix EducationM temp1
	eststo EducationM

	estpost gtabstat exper [w=orgwgt] if female==0, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Experience
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix ExperienceM temp1
	eststo ExperienceM

	estpost gtabstat lrwage3 if female==0, statistics(count) by(year) 
	mat temp1 = e(n)
	mat temp1 = temp1'
	matrix colnames temp1 = No_Obs
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix ObsM temp1
	eststo ObsM

	#delimit ;
	esttab LogRmw LogRwM UnionM NonwhiteM EducationM ExperienceM ObsM,
	cells( "LogRmw(pattern(1 0 0 0 0 0 0) fmt(%20.2fc) lab(Real Min. Wage)) 
			LogRwM(pattern(0 1 0 0 0 0 0) fmt(%20.2fc) lab(Real Wage))
			UnionM(pattern(0 0 1 0 0 0 0) fmt(%20.2fc) lab(Union))
		 NonwhiteM(pattern(0 0 0 1 0 0 0) fmt(%20.2fc) lab(Nonwhite))
		EducationM(pattern(0 0 0 0 1 0 0) fmt(%20.2fc) lab(Education))
	   ExperienceM(pattern(0 0 0 0 0 1 0) fmt(%20.2fc) lab(Experience))
			  ObsM(pattern(0 0 0 0 0 0 1) fmt(%20.0fc) lab(No. Obs.))")
			  noobs mlabels(,none)
			  nostar nonum nogaps nodepvars compress ;
	#delimit cr

}

/// Excel Summary Stats females
{
	preserve
	collapse  (mean) Real_Min_Wage=rmwage (mean) Real_Wage=rwage3 /*
			*/(mean) Manufacturing=manuf /*
			*/(mean) Union=union (mean) Nonwhite=nonwhite /*
			*/(mean) Education=years_educ (mean) Experience=exper (count) No_Obs=lrwage3 /*
			*/(p1) p1=lrwage3 (p10) p10=lrwage3 (p25) p25=lrwage3 (p50) p50=lrwage3 /*
			*/(p75) p75=lrwage3 (p90) p90=lrwage3 (p99) p99=lrwage3 [w=orgwgt] if female==1, by(year)
			
	gen Log_Real_Wage=ln(Real_Wage)
	gen Log_Real_Min_Wage=ln(Real_Min_Wage)

	keep Log_Real_Min_Wage Log_Real_Wage Manufacturing Union Nonwhite Education Experience No_Obs p1 p10 p25 p50 p75 p90 p99
	order Log_Real_Min_Wage Log_Real_Wage Manufacturing Union Nonwhite Education Experience No_Obs p1 p10 p25 p50 p75 p90 p99		
	ds
	cd `Tables'
	mkmat `r(varlist)' , mat(S)
	xml_tab S, save("Summary_Stat.xls") sheet(Females) append
	restore
}

/// Table Summary Stats females
{
	estpost gtabstat rmwage [w=orgwgt] if female==1, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'

	mata: st_matrix("ltemp1", log(st_matrix("temp1")))
	mat temp1 = temp1,ltemp1
	mat temp1 = temp1[1 .. rowsof(temp1), 2]
	matrix colnames temp1 = Log_Real_Min_Wage
	matrix output = temp1

	mat temp1 = temp1'
	estadd matrix LogRmwF temp1
	eststo LogRmwF 

	estpost gtabstat rwage3 [w=orgwgt] if female==1, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'

	mata: st_matrix("ltemp1", log(st_matrix("temp1")))
	mat temp1 = temp1,ltemp1
	mat temp1 = temp1[1 .. rowsof(temp1), 2]
	matrix colnames temp1 = Log_Real_Wage
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix LogRwF temp1
	eststo LogRwF 

	estpost gtabstat union [w=orgwgt] if female==1, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Union
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix UnionF temp1
	eststo UnionF

	estpost gtabstat nonwhite [w=orgwgt] if female==1, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Nonwhite
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix NonwhiteF temp1
	eststo NonwhiteF

	estpost gtabstat years_educ [w=orgwgt] if female==1, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Education
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix EducationF temp1
	eststo EducationF

	estpost gtabstat exper [w=orgwgt] if female==1, statistics(mean) by(year) 
	mat temp1 = e(mean)
	mat temp1 = temp1'
	matrix colnames temp1 = Experience
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix ExperienceF temp1
	eststo ExperienceF

	estpost gtabstat lrwage3 if female==1, statistics(count) by(year) 
	mat temp1 = e(n)
	mat temp1 = temp1'
	matrix colnames temp1 = No_Obs
	matrix coljoinbyname output = output  temp1

	mat temp1 = temp1'
	estadd matrix ObsF temp1
	eststo ObsF

	#delimit ;
	esttab LogRmw LogRwF UnionF NonwhiteF EducationF ExperienceF ObsF,
	cells( "LogRmw(pattern(1 0 0 0 0 0 0) fmt(%20.2fc) lab(Real Min. Wage)) 
			LogRwF(pattern(0 1 0 0 0 0 0) fmt(%20.2fc) lab(Real Wage))
			UnionF(pattern(0 0 1 0 0 0 0) fmt(%20.2fc) lab(Union))
		 NonwhiteF(pattern(0 0 0 1 0 0 0) fmt(%20.2fc) lab(Nonwhite))
		EducationF(pattern(0 0 0 0 1 0 0) fmt(%20.2fc) lab(Education))
	   ExperienceF(pattern(0 0 0 0 0 1 0) fmt(%20.2fc) lab(Experience))
			  ObsF(pattern(0 0 0 0 0 0 1) fmt(%20.0fc) lab(No. Obs.))")
			  noobs mlabels(,none)
			  nostar nonum nogaps nodepvars compress ;
	#delimit cr

}


///summary stats table
{
	#delimit ;
	esttab 	LogRmw Manufacturing
			LogRwM UnionM NonwhiteM EducationM ExperienceM ObsM 
			LogRwF UnionF NonwhiteF EducationF ExperienceF ObsF,
		cells( "LogRmw(pattern(1 0 0 0 0 0 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Real Min. Wage))
		 Manufacturing(pattern(0 1 0 0 0 0 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Manufacturing))
				LogRwM(pattern(0 0 1 0 0 0 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Real Wage M))
				UnionM(pattern(0 0 0 1 0 0 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Union M))
			 NonwhiteM(pattern(0 0 0 0 1 0 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Nonwhite M))
			EducationM(pattern(0 0 0 0 0 1 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Education M))
		   ExperienceM(pattern(0 0 0 0 0 0 1 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Experience M))
				  ObsM(pattern(0 0 0 0 0 0 0 1 0 0 0 0 0 0) fmt(%20.0fc) lab(No. Obs. M))
				  
				LogRwF(pattern(0 0 0 0 0 0 0 0 1 0 0 0 0 0) fmt(%20.2fc) lab(Real Wage F))
				UnionF(pattern(0 0 0 0 0 0 0 0 0 1 0 0 0 0) fmt(%20.2fc) lab(Union F))
			 NonwhiteF(pattern(0 0 0 0 0 0 0 0 0 0 1 0 0 0) fmt(%20.2fc) lab(Nonwhite F))
			EducationF(pattern(0 0 0 0 0 0 0 0 0 0 0 1 0 0) fmt(%20.2fc) lab(Education F))
		   ExperienceF(pattern(0 0 0 0 0 0 0 0 0 0 0 0 1 0) fmt(%20.2fc) lab(Experience F))
				  ObsF(pattern(0 0 0 0 0 0 0 0 0 0 0 0 0 1) fmt(%20.0fc) lab(No. Obs. F))")
				  noobs nostar nonum nogaps nodepvars compress 
				  mlabels(, lhs("Year")) 
				  collabels(,none) 
				  drop(1979 2016 2017 2018 2019 2020)
				  extracols(9);
	#delimit cr
	
	
	*** Latex
	cd "`Tables'"

	local nxl= char(10)
	local numbers = "\begin{tabular}{cccccccccccccccc}" + "`nxl'"
	local numbers = "`numbers'" + "\hline\hline" + "`nxl'"
	local numbers = "`numbers'" + "		 & 	   &	& 	\multicolumn{6}{c}{Men}				       & 	\multicolumn{6}{c}{Women} 	\\ 	\cline{4-9}  \cline{11-16}"	+ "`nxl'"
	local numbers = "`numbers'" + "\multicolumn{1}{c}{\footnotesize{Year}} & \multicolumn{1}{p{1.3cm}}{\centering \footnotesize{Log Real Minimum Wage$^A$}} & \multicolumn{1}{p{1.3cm}}{\centering \footnotesize{Manufact. Employ.}}&\multicolumn{1}{p{1.3cm}}{\centering \footnotesize{Log Real Wage$^A$}} & \multicolumn{1}{c}{\footnotesize{Union$^B$}} & \multicolumn{1}{c}{\footnotesize{Nonwhite}} & \multicolumn{1}{c}{\footnotesize{Education}} & \multicolumn{1}{p{1.3cm}}{\footnotesize{Experience$^C$}} & \multicolumn{1}{c}{\footnotesize{No. Obs.}} & \multicolumn{1}{p{0.1pt}}{} & \multicolumn{1}{p{1.3cm}}{\centering \footnotesize{Log Real Wage$^A$}} & \multicolumn{1}{c}{\footnotesize{Union$^B$}} & \multicolumn{1}{c}{\footnotesize{Nonwhite}} & \multicolumn{1}{c}{\footnotesize{Education}} & \multicolumn{1}{p{1.3cm}}{\footnotesize{Experience$^C$}} & \multicolumn{1}{c}{\footnotesize{No. Obs.}} \\ "

	local last = "\hline\hline" + "`nxl'"
	local last = "`last'" + "\multicolumn{16}{p{\linewidth}}{ \scriptsize{$^A$ 2020 Constant Dollars}} \\" + "`nxl'"
	local last = "`last'" + "\multicolumn{16}{p{\linewidth}}{ \scriptsize{$^B$ Union status of workers was not collected in the outgoing rotation group supplements from 1980 to 1982. However, using the May pension supplement it may be possible estimate this summary statistic for a subsample of the population}} \\" + "`nxl'"
	local last = "`last'" + "\multicolumn{16}{p{\linewidth}}{ \scriptsize{$^C$ Potential experience is computed as age - years of education - 5}} \\" + "`nxl'"
	local last = "`last'" + "\end{tabular}"

	#delimit ;
	esttab 	LogRmw Manufacturing
			LogRwM UnionM NonwhiteM EducationM ExperienceM ObsM 
			LogRwF UnionF NonwhiteF EducationF ExperienceF ObsF using SumStats,
		cells( "LogRmw(pattern(1 0 0 0 0 0 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Real Min. Wage))
		 Manufacturing(pattern(0 1 0 0 0 0 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Manufacturing))
				LogRwM(pattern(0 0 1 0 0 0 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Real Wage M))
				UnionM(pattern(0 0 0 1 0 0 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Union M))
			 NonwhiteM(pattern(0 0 0 0 1 0 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Nonwhite M))
			EducationM(pattern(0 0 0 0 0 1 0 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Education M))
		   ExperienceM(pattern(0 0 0 0 0 0 1 0 0 0 0 0 0 0) fmt(%20.2fc) lab(Experience M))
				  ObsM(pattern(0 0 0 0 0 0 0 1 0 0 0 0 0 0) fmt(%20.0fc) lab(No. Obs. M))
				  
				LogRwF(pattern(0 0 0 0 0 0 0 0 1 0 0 0 0 0) fmt(%20.2fc) lab(Real Wage F))
				UnionF(pattern(0 0 0 0 0 0 0 0 0 1 0 0 0 0) fmt(%20.2fc) lab(Union F))
			 NonwhiteF(pattern(0 0 0 0 0 0 0 0 0 0 1 0 0 0) fmt(%20.2fc) lab(Nonwhite F))
			EducationF(pattern(0 0 0 0 0 0 0 0 0 0 0 1 0 0) fmt(%20.2fc) lab(Education F))
		   ExperienceF(pattern(0 0 0 0 0 0 0 0 0 0 0 0 1 0) fmt(%20.2fc) lab(Experience F))
				  ObsF(pattern(0 0 0 0 0 0 0 0 0 0 0 0 0 1) fmt(%20.0fc) lab(No. Obs. F))")
				  prehead("`numbers'") 
				  noobs nostar nonum nogaps nodepvars compress 
				  mlabels(, none)
				  collabels(,none)
				  extracols(9)
				  postfoot("`last'")
				  drop(1979 2016 2017 2018 2019 2020)
				  tex replace; 
	#delimit cr
}


***
*** Data for analysis in R
***
{
	use "$US_Ineq_Repl/Processed/DATA.dta", replace
	*log of real wage
	gen lrwage3=ln(rwage3) 
	keep if year==1986 | year==2015 
	
	order rwage3 lrwage3
	
	order ind21, last
	tab ind21, gen(indu)
	
	order ee_cl, last
	tab ee_cl, gen(ee)
	
	order state, last
	tab state, gen(state)
	
	order educ_cl, last
	tab educ_cl, gen(educ)
	
	order exp_cl, last
	tab exp_cl, gen(expe)
	
	order exper, last
	gen exper2=exper^2
	gen exper3=exper^3
	gen exper4=exper^4

	reg lrwage3 union pubsect manuf nonwhite female partte married smsa c.exper exper2 i.exp_cl i.educ_cl  edex i.exp_cl#i.educ_cl i.ind21 [pweight= orgwgt ]
	reg lrwage3 union pubsect manuf nonwhite female partte married smsa exper exper2 expe2-expe4 educ2-educ5  edex ee2-ee20 indu2-indu20 state2-state51 if year==1986 [pweight= orgwgt] 

	saveold "$US_Ineq_Repl/Processed/Data86-15.dta", replace
}